package view;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.util.*;


public class RowLayout implements LayoutManager {
	private java.util.List<Component> components;
	private Dimension preferredSize;

	public RowLayout() {
		components = new Vector<Component>();
	}

	public void layoutContainer(Container c) {
		synchronized (c.getTreeLock()) {
			int width = c.getSize().width;
			int i=0, h=0;
			for (Component comp : c.getComponents()) {
				if (!comp.isVisible()) continue;
				Dimension d = comp.getPreferredSize();
				int cw = Math.max(width-2,comp.getMinimumSize().width);
				if (!comp.getSize().equals(new Dimension(cw,d.height))) {
					comp.setSize(cw,d.height);
				}
				Point l = new Point((c.getSize().width-comp.getSize().width)/2,h);
				if (!comp.getLocation().equals(l)) {
					comp.setLocation(l);
				}
				i++;
				h += comp.getSize().height;
			}
		}
	}

	public Dimension minimumLayoutSize(Container c) {
		synchronized (c.getTreeLock()) {
			int w=0, h=0, i=0;
			if (c.getComponentCount()==0) return new Dimension(10,10);
			for (Component comp : c.getComponents()) {
				if (!comp.isVisible()) continue;
				h += comp.getMinimumSize().height;
				w = comp.getMinimumSize().width;
				i++;
			}
			return new Dimension(w,h);
		}
	}

	public Dimension preferredLayoutSize(Container c) {
		synchronized (c.getTreeLock()) {
			int w=0, h=0, i=0;
			if (c.getComponentCount()==0) return new Dimension(10,10);
			for (Component comp : c.getComponents()) {
				if (!comp.isVisible()) continue;
				h += comp.getPreferredSize().height;
				w = Math.max(w,comp.getPreferredSize().width+2);
				i++;
			}
			preferredSize = new Dimension(w,h);
			return preferredSize;
		}
	}

	public void removeLayoutComponent(Component c) {
		synchronized (c.getTreeLock()) {
			c.getParent().repaint(c.getLocation().x,c.getLocation().y,
					c.getSize().width,c.getSize().height);
		}
	}

	public void addLayoutComponent(String n,Component c) {}
}
